package edu.pitt.dbmi.odie.gapp.gwt.server.util.marginizer;

import java.io.File;
import java.io.IOException;

import edu.upmc.opi.caBIG.common.CaBIG_ReadWriteTextFile;

public class ODIE_Marginizer {

	public static final int RIGHT_MARGIN = 80;

	private String inputFileAsString = null;
	private String outputFileAsString = null;
	
	private char[] fileChars = null ;
	private int fileCursor = 0;

	/**
	 * Breaks up long lines at a user specified right margin
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			String testFileName = args[0] ;
			String testFileOutputName = args[1];
			ODIE_Marginizer odieMarginizer = new ODIE_Marginizer();
			odieMarginizer.setInputFileAsString(CaBIG_ReadWriteTextFile
					.getContents(new File(testFileName)));
			odieMarginizer.execute() ;
			File outputFile = new File(testFileOutputName);
			if (!outputFile.exists()) {
					outputFile.createNewFile();
			}
			CaBIG_ReadWriteTextFile.setContents(outputFile, odieMarginizer.getOutputFileAsString());
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public ODIE_Marginizer() {
	}

	public void execute() {
		if (this.inputFileAsString != null) {
			this.fileChars = this.inputFileAsString.toCharArray();
			this.fileCursor = 0 ;
			StringBuffer fileBuffer = new StringBuffer();
			StringBuffer lineBuffer = new StringBuffer();
			while (this.fileCursor < fileChars.length) {
				char nextChar = fileChars[fileCursor++];
				lineBuffer.append(nextChar);
				if (nextChar == '\n') {
					fileBuffer.append(lineBuffer);
					lineBuffer = new StringBuffer();
				} else {
					if (lineBuffer.length() > RIGHT_MARGIN) {
						char overflowChar = fileChars[--fileCursor] ;
						char marginalChar = fileChars[--fileCursor];
						lineBuffer.deleteCharAt(lineBuffer.length() - 1);
					    if (Character.isWhitespace(overflowChar)) {
					    	fileCursor = fileCursor + 2 ; // Don't carry over the space at beginning
					    }
					    else if (Character.isWhitespace(marginalChar)) {
					    	fileCursor = fileCursor + 1 ;
					    }
					    else {
					    	backUpToWhiteSpace(lineBuffer) ;
					    }
						lineBuffer.append('\n');
						fileBuffer.append(lineBuffer);
						lineBuffer = new StringBuffer();
					}
					
				}
			}
			if (lineBuffer.length() > 0) {
				fileBuffer.append(lineBuffer);
			}
			this.outputFileAsString = fileBuffer.toString();
		}
	}

	private void backUpToWhiteSpace(StringBuffer lineBuffer) {
		int numberOfNonWhiteSpaces = 0 ;
		for (int idx = lineBuffer.length() - 1 ; idx >= 0 ; idx--) {
			if (!Character.isWhitespace(lineBuffer.charAt(idx))) {
				numberOfNonWhiteSpaces++ ;
			}
			else {
				break ;
			}
		}
		if (numberOfNonWhiteSpaces < lineBuffer.length()) {
			lineBuffer.delete(lineBuffer.length()-numberOfNonWhiteSpaces, lineBuffer.length()) ;
			fileCursor = fileCursor - numberOfNonWhiteSpaces + 1 ;
		}
		else {
			fileCursor++ ;
		}	
	}

	public String getInputFileAsString() {
		return inputFileAsString;
	}

	public void setInputFileAsString(String inputFileAsString) {
		this.inputFileAsString = inputFileAsString;
	}

	public String getOutputFileAsString() {
		return outputFileAsString;
	}

	public void setOutputFileAsString(String outputFileAsString) {
		this.outputFileAsString = outputFileAsString;
	}

}
